﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using AmkeniCrossLoginApplication.Data_Access;

namespace Amkeni_System.CO.Edit
{
    public partial class AddProject : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            /*if (User.IsInRole("IP"))
            {
                MasterPageFile = "~/MasterPages/Project.Master";
            }
            if (User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/Programme.Master";
            }
            if (User.IsInRole("Administrator"))
            {
                MasterPageFile = "~/MasterPages/CO.Master";
            }
            if (User.IsInRole("IP") && User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/ProgProject.Master";
            }*/
        }

        Amkeni_DatabaseEntities db = new Amkeni_DatabaseEntities();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            try
            {
                //=====================================+
                //  Add a new Implementing Partner     |
                //=====================================+
                implementing_partner newIP = new implementing_partner();
                newIP.outcome_id = int.Parse(ProgrammeList.SelectedValue.Trim());
                newIP.region_id = int.Parse(RegionList.SelectedValue.Trim());
                newIP.name = ProjectTextBox.Text.Trim();
                newIP.abbreviation = AbbrevTextBox.Text.Trim();
                newIP.ip_classification_id = int.Parse(ClassificationList.SelectedValue.Trim());
                db.implementing_partner.Add(newIP);

                if (RFPList.SelectedValue != null)
                {
                    implementing_partner_metadata ipMetadata = new implementing_partner_metadata();
                    ipMetadata.RFP_id = int.Parse(RFPList.SelectedValue.Trim());
                    ipMetadata.implementing_partner = newIP;
                    db.implementing_partner_metadata.Add(ipMetadata);
                }
                db.SaveChanges();
                Response.Redirect("~/Amkeni_System/CO/Progs_Projects.aspx", true);
            }
            catch (Exception ex)
            {
                error.Text = "Database Error: " + ex.Message + "<br /> " + ex.StackTrace;
                error.Visible = true;
            }
            //finally
            //{
            //    db.SaveChanges();
            //}
        }
    }
}